use hysteria_api::ApiServer;
use serde::Deserialize;
use std::net::SocketAddr;
use tracing::info;

#[derive(Debug, Deserialize)]
struct AdminConfig {
    listen: SocketAddr,
    database: DatabaseConfig,
}

#[derive(Debug, Deserialize)]
struct DatabaseConfig {
    path: String,
}

pub async fn run(config_path: &str) -> anyhow::Result<()> {
    info!("Loading admin configuration from: {}", config_path);

    let content = std::fs::read_to_string(config_path)?;
    let config: AdminConfig = serde_yaml::from_str(&content)?;

    let database_url = format!("sqlite:{}", config.database.path);

    info!("Starting admin panel...");
    let server = ApiServer::new(config.listen, &database_url).await?;
    server.serve().await?;

    Ok(())
}
